package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CancelOrderRequest;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.remote.ProductApiClient;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

import static com.cskaoyan.order.constant.OrderConstants.ORDER_ID;
import static com.cskaoyan.order.constant.OrderConstants.ORDER_ITEM_ID;

/**
 * @Description:
 * @author: jzc
 * @since: 2022/10/25 20:15
 **/
@RestController
@RequestMapping("shopping")
public class OrderController {

    // @Reference(timeout=3000,check=false)
    @Autowired
    private OrderCoreService orderCoreService;
    // @Reference(timeout=3000,check=false)
    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;
    @Autowired
    private RedissonClient redissonClient;



    @PostMapping("order")
    public ResponseData order(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest){
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        if (object==null||object.isEmpty()){
            throw new BizException(OrderRetCode.USER_LOGIN_FAIL.getCode(),
                    OrderRetCode.USER_LOGIN_FAIL.getMessage());
        }
        Long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);
        //设置UniqueKey
        String nextSeq = globalIdGeneratorUtil.getNextSeq("order_id", 1);
        // request.setUniqueKey(UUID.randomUUID().toString());
        request.setUniqueKey(nextSeq);
        String orderId = orderCoreService.createOrder(request);
        if (orderId==null||orderId.isEmpty()){
            return new ResponseUtil<>().setErrorMsg("订单创建失败");
        }
        return new ResponseUtil<>().setData(orderId);
    }
    @GetMapping("order")
    public ResponseData getOrder(OrderListRequest orderListRequest, HttpServletRequest servletRequest){
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        if (object==null||object.isEmpty()){
            throw new BizException(OrderRetCode.USER_LOGIN_FAIL.getCode(),
                    OrderRetCode.USER_LOGIN_FAIL.getMessage());
        }
        Long uid = Long.parseLong(object.get("uid").toString());
        orderListRequest.setUserId(uid);
        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);
        return new ResponseUtil<>().setData(orderListResponse);
    }
    @GetMapping("order/{id}")
    public ResponseData getOrderDetail(@PathVariable String id,HttpServletRequest servletRequest){
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        if (object==null||object.isEmpty()){
            throw new BizException(OrderRetCode.USER_LOGIN_FAIL.getCode(),
                    OrderRetCode.USER_LOGIN_FAIL.getMessage());
        }
        // Long uid = Long.parseLong(object.get("id").toString());
        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(id);
        return new ResponseUtil<>().setData(orderDetailResponse);
    }
    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest, HttpServletRequest servletRequest) {
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        if (object==null||object.isEmpty()){
            throw new BizException(OrderRetCode.USER_LOGIN_FAIL.getCode(),
                    OrderRetCode.USER_LOGIN_FAIL.getMessage());
        }
        // Long uid = Long.parseLong(object.get("id").toString());
        orderCoreService.cancelOrder(cancelOrderRequest.getOrderId());
        return new ResponseUtil<>().setData(null);
    }

    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable String id, HttpServletRequest servletRequest) {
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        // Long uid = Long.parseLong(object.get("id").toString());
        if (object==null||object.isEmpty()){
            throw new BizException(OrderRetCode.USER_LOGIN_FAIL.getCode(),
                    OrderRetCode.USER_LOGIN_FAIL.getMessage());
        }



        orderCoreService.deleteOrder(id);
        return new ResponseUtil<>().setData(null);
    }
}
